package com.vnv.infrastructure.email.service;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import uk.ltd.getahead.dwr.WebContextFactory;
import cn.agiledata.bank.common.util.Constant;

import com.vnv.infrastructure.util.ConvertUtil;
import com.vnv.infrastructure.util.DateUtil;

public class VNVMail implements Serializable {
	
	private static final long serialVersionUID = 817628744254662190L;

	public static String POST_MASTER = Constant.frontconfig
			.getString("mailFrom");

	public static String TARGET_PAGE_PATH = "/main.htm#activate";
	public static String TARGET_PAGE_PAY_PATH = "/activate.do";
	public static String TARGET_PAGE_MUTUAL_PATH = "/mutual/main.do?method=toActivate";

	public static String ACTIVATE = "activateMail";

	public static VNVMail MAIL_REGISTER = new VNVMail();
	private String templateName;
	private Object context;
	private String emailTo;
	private String emailFrom = Constant.frontconfig.getString("mailFrom");
	private String subject;
	private List attachmentUrl;
	private String[] emailToList;

	static {
		MAIL_REGISTER.setEmailFrom(Constant.frontconfig.getString("mailFrom"));
		MAIL_REGISTER.setSubject("注册信");
		MAIL_REGISTER.setTemplateName("");
	}

	public static VNVMail createActivateMail(String url, String email,
			String activeCode, String loginPass, String payPass, String project) {
		String path = "";
		try {
			if (project == null)
				path = TARGET_PAGE_PATH;
			else if (project.equals("webpay")) {
				path = TARGET_PAGE_PAY_PATH;
			}
		} catch (RuntimeException localRuntimeException) {
		}
		StringBuffer activeURL = new StringBuffer();
		activeURL.append(url);
		activeURL.append(path);
		if (activeURL.indexOf("?") == -1)
			activeURL.append("?");
		else {
			activeURL.append("&");
		}

		activeURL.append("email=" + email);
		activeURL.append("&");
		activeURL.append("activeCode=" + activeCode);

		VNVMail mail = new VNVMail();
		mail.setEmailTo(email);
		mail.setEmailFrom(POST_MASTER);

		Map ctx = new HashMap();
		ctx.put("account", email);
		ctx.put("activeCode", activeCode);
		ctx.put("activeURL", activeURL.toString());
		ctx.put("loginPass", loginPass);
		ctx.put("payPass", payPass);
		ctx.put("urlpath", url);
		ctx.put("now", DateUtil.date2String(new Date()));
		mail.setContext(ctx);
		mail.setSubject("您的账号的激活信");
		mail.setTemplateName("mail-accountActivate.vm");

		return mail;
	}

	public static VNVMail createActivateMail(String email, String activeCode,
			String loginPass, String payPass, String project) {
		String url = "";

		String path = "";
		try {
			HttpServletRequest request = WebContextFactory.get()
					.getHttpServletRequest();
			url = ConvertUtil.getRequestURL(request);
			if (project == null)
				path = TARGET_PAGE_PATH;
			else if (project.equals("webpay")) {
				path = TARGET_PAGE_PAY_PATH;
			}
		} catch (RuntimeException localRuntimeException) {
		}
		StringBuffer activeURL = new StringBuffer();
		activeURL.append(url);
		activeURL.append(path);
		if (activeURL.indexOf("?") == -1)
			activeURL.append("?");
		else {
			activeURL.append("&");
		}

		activeURL.append("email=" + email);
		activeURL.append("&");
		activeURL.append("activeCode=" + activeCode);

		VNVMail mail = new VNVMail();
		mail.setEmailTo(email);
		mail.setEmailFrom(POST_MASTER);

		Map ctx = new HashMap();
		ctx.put("account", email);
		ctx.put("activeCode", activeCode);
		ctx.put("activeURL", activeURL.toString());
		ctx.put("urlpath", url);
		ctx.put("loginPass", loginPass);
		ctx.put("payPass", payPass);
		ctx.put("now", DateUtil.date2String(new Date()));
		mail.setContext(ctx);
		mail.setSubject("您的账号的激活信");
		mail.setTemplateName("mail-accountActivate.vm");

		return mail;
	}

	public static VNVMail createPasswordMail(String email, String loginPass,
			String payPass) {
		VNVMail mail = new VNVMail();
		mail.setEmailTo(email);
		mail.setEmailFrom(POST_MASTER);

		Map ctx = new HashMap();
		ctx.put("account", email);
		ctx.put("loginPass", loginPass);
		ctx.put("payPass", payPass);

		mail.setContext(ctx);
		mail.setSubject("您的账号的密码");
		mail.setTemplateName("mail-accountPassword.vm");

		return mail;
	}

	public static VNVMail createRuleMail(String email, String ruleRemark,
			String amount) {
		VNVMail mail = new VNVMail();
		mail.setEmailTo(email);
		mail.setEmailFrom(POST_MASTER);

		Map ctx = new HashMap();
		ctx.put("account", email);
		ctx.put("activeURL", ruleRemark);
		ctx.put("loginPass", amount);
		ctx.put("now", DateUtil.date2String(new Date()));
		mail.setContext(ctx);
		mail.setSubject("您的预警信");
		mail.setTemplateName("mail-moneyRole.vm");

		return mail;
	}

	public static VNVMail createRuleMail2(String email, String ruleRemark,
			String amount, String account) {
		VNVMail mail = new VNVMail();
		mail.setEmailTo(email);
		mail.setEmailFrom(POST_MASTER);

		Map ctx = new HashMap();
		ctx.put("account", account);
		ctx.put("activeURL", ruleRemark);
		ctx.put("loginPass", amount);
		ctx.put("now", DateUtil.date2String(new Date()));
		mail.setContext(ctx);
		mail.setSubject("您的预警信");
		mail.setTemplateName("mail-moneyRole.vm");

		return mail;
	}

	public String[] getEmailToList() {
		return this.emailToList;
	}

	public void setEmailToList(String[] emailToList) {
		this.emailToList = emailToList;
	}

	public List getAttachmentUrl() {
		return this.attachmentUrl;
	}

	public void setAttachmentUrl(List attachmentUrl) {
		this.attachmentUrl = attachmentUrl;
	}

	public String getEmailTo() {
		return this.emailTo;
	}

	public void setEmailTo(String emailAddr) {
		this.emailTo = emailAddr;
	}

	public String getSubject() {
		return this.subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getTemplateName() {
		return this.templateName;
	}

	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}

	public Object getContext() {
		return this.context;
	}

	public void setContext(Object values) {
		this.context = values;
	}

	public String getEmailFrom() {
		return this.emailFrom;
	}

	public void setEmailFrom(String emailFrom) {
		this.emailFrom = emailFrom;
	}
}